/*
 * logbook.c
 *
 * Created: 13.05.2014 16:25:06
 *  Author: markus.hufschmid
 */ 
#include "defines.h"
#include "eeprom.h"
#include "logbook.h"

uint16_t get_nr_of_dives(void){
	uint16_t nr_l, nr_h;
	
	nr_h = EEPROM_read(0x0000);
	nr_l = EEPROM_read(0x0001);
	
	return ((nr_h << 8) + nr_l);
}

void write_nr_of_dives(uint16_t number){
	uint8_t nr_l, nr_h;
	
	nr_h = number >> 8;
	nr_l = number & 0xFF;
	
	EEPROM_write(0x0000, nr_h);
	EEPROM_write(0x0001, nr_l);
}


void write_log_info(log_info_t log_info){
	uint16_t address;
	
	if (log_info.nr < 256)
	{
		address = log_info.nr << 4;
		EEPROM_write(address++, log_info.nr >> 8);
		EEPROM_write(address++, log_info.nr & 0xFF);
		EEPROM_write(address++, log_info.day);
		EEPROM_write(address++, log_info.month);
		EEPROM_write(address++, log_info.year >> 8);
		EEPROM_write(address++, log_info.year & 0xFF);
		EEPROM_write(address++, log_info.hour);
		EEPROM_write(address++, log_info.min);
		EEPROM_write(address++, log_info.max_depth_m);
		EEPROM_write(address++, log_info.max_depth_dm);
		EEPROM_write(address++, log_info.min_temp);
		EEPROM_write(address++, log_info.duration >> 8);
		EEPROM_write(address++, log_info.duration & 0xFF);
	}
}

log_info_t read_log_info(uint16_t number){
	uint16_t address;
	log_info_t log_info;
	uint16_t temp_low, temp_high;
	
	if (number < 256)
	{
		address = number << 4;
		temp_high = EEPROM_read(address++);
		temp_low = EEPROM_read(address++);
		log_info.nr = (temp_high << 8) + temp_low;
		log_info.day = EEPROM_read(address++);
		log_info.month = EEPROM_read(address++);
		temp_high = EEPROM_read(address++);
		temp_low = EEPROM_read(address++);
		log_info.year = (temp_high << 8) + temp_low;
		log_info.hour = EEPROM_read(address++);
		log_info.min = EEPROM_read(address++);
		log_info.max_depth_m = EEPROM_read(address++);
		log_info.max_depth_dm = EEPROM_read(address++);
		log_info.min_temp = EEPROM_read(address++);
		temp_high = EEPROM_read(address++);
		temp_low = EEPROM_read(address++);
		log_info.duration = (temp_high << 8) + temp_low;
	}
	return log_info;
}
